﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;

namespace LowLevelGraphics.Filter
{
    /// <summary>
    /// 
    /// </summary>
    public class Difference2 : BaseForeBackgroundColorFilter, ITwoSourceFilter
    {
        protected int r = 0;
        protected int g = 0;
        protected int b = 0;

        /// <summary>
        /// Initializes a new instance of the <see cref="Difference2"/> class.
        /// </summary>
        public Difference2()
            : this(0, 0, 0)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Difference2"/> class.
        /// </summary>
        /// <param name="_r">The _r.</param>
        /// <param name="_g">The _g.</param>
        /// <param name="_b">The _b.</param>
        public Difference2(int _r, int _g, int _b)
        {
            r = _r;
            g = _g;
            b = _b;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Difference2"/> class.
        /// </summary>
        /// <param name="_difference2">The _difference2.</param>
        internal Difference2(Difference2 _difference2)
        {
            this.r = _difference2.r;
            this.g = _difference2.g;
            this.b = _difference2.b;
        }

        /// <summary>
        /// Execute this Filter
        /// A Debug Statement is written if a concrete Filter is called
        /// </summary>
        /// <param name="_bitmap"></param>
        /// <returns></returns>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            return Process((UnsafeBitmap)_bitmap.Clone(), _bitmap);
        }

        /// <summary>
        /// Shows only the differences left. Regions that are equal
        /// in both bitmaps will be blacked out
        /// </summary>
        /// <param name="_bmpimg">The _bmpimg.</param>
        /// <param name="_bmp">The _BMP.</param>
        /// <returns></returns>
        public UnsafeBitmap Process(UnsafeBitmap _bmpimg, UnsafeBitmap _bmp)
        {
            UnsafeBitmap bmpimg = _bmpimg;
            UnsafeBitmap bmp = _bmp;

            int nWidth = bmpimg.Width;
            int nHeight = bmp.Height;

            UnsafeBitmap _bmpresult = new Bitmap(nWidth, nHeight);
            UnsafeBitmap bmpResult = new UnsafeBitmap(_bmpresult);
            Color color1 = Color.Transparent;
            Color color2 = Color.Transparent;
            for (int y = 0; y < nHeight; y++)
            {
                for (int x = 0; x < nWidth; x++)
                {
                    color1 = bmp.GetPixel(x, y);
                    color2 = bmpimg.GetPixel(x, y);
                    if (ColorFunctions.IsInColorTolerance(color1, color2, r, g, b))
                    {
                        bmpResult.SetPixel(x, y, m_ColorForeground);
                    }
                    else
                    {
                        bmpResult.SetPixel(x, y, m_ColorBackground);
                    }
                }
            }

            bmp.Dispose();
            bmpimg.Dispose();
            bmpResult.Dispose();

            return _bmpresult;
        }

        /// <summary>
        /// This is used to clone a filter, each filter must implement it's own clone functionality
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new Difference2(this);
        }
    }
}
